package org.candan.spider.util;

import java.io.File;

import org.apache.log4j.Logger;

/**
 * @author zhangsc
 *
 */
public class SLog {
	
	private  Logger logger = null;
	
	public Logger getLogger() {
		return logger;
	}
	
	public SLog(){
		this("",null,"ProjectRun.log");
	}
	
	public SLog(String className){
		this(className,null,"ProjectRun.log");
	}
	
	public SLog(String className,String logDir, String logName){
		//如果不设置日志 目录，则设置为 当前工程的目录
		if(logDir == null || "".equals(logDir.trim())){
			logDir = getProDir();
		}else{
			//如果设置了日志的目录，检测测目录是否 存在，否则建立
			File dir = new File(logDir);
			if(!dir.exists()){
				dir.mkdirs();
			}
		}
		//设置log4j的配置文件 的 日志文件，此操作要先于 Logger.getLogger(className)
		System.setProperty("LOG_FILE", logDir+"/"+logName);
		logger = Logger.getLogger(className);
	}
	
	/**
	 * @category 得到当前工程目录
	 * @return
	 */
	private String getProDir(){
		String dir = PropertyParse.getValue("preproccess.log");
		if(!new File(dir).exists()){
			new File(dir).mkdirs();
		}
		return dir;
//		return new File(getClass().getProtectionDomain().getCodeSource().getLocation().getFile()).getParent();
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		SLog log1 = new SLog();
		System.out.println("log 目录 ："+System.getProperty("LOG_FILE"));
		log1.getLogger().info("asdfasf");
		
		SLog log2 = new SLog("util.Class");
		System.out.println("log 目录 ："+System.getProperty("LOG_FILE"));
		log2.getLogger().info("asdfasf");

		SLog log3 = new SLog("util.Clas",null,"lllll.log");
		System.out.println("log 目录 ："+System.getProperty("LOG_FILE"));
		log3.getLogger().info("asdfasf");
		
		SLog log4 = new SLog("util.Clas","D:\\sxt","lllll.log");
		System.out.println("log 目录 ："+System.getProperty("LOG_FILE"));
		log4.getLogger().info("asdfasf");
	}

}
